package client;

/*
 * FlughafeninformationssystemUI.java
 *
 * Created on 30. Mai 2011, 08:58
 */



import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.rmi.RemoteException;
import java.util.Map;
import server.ejb.beans.*;
import adapter.IAdapter;
import adapter.ICallbackClient;

import factory.Factory;

/**
 * Class for GUI.
 */
public class ClientUI extends javax.swing.JFrame implements ICallbackClient, ItemListener, ActionListener {
	private static final long serialVersionUID = 0L;
	
	private int selectedLandingWayId = -1;
	private int selectedFlugzeugId = -1;

	private ImagePanel ipanel = null;
	/** Creates new form FlughafeninformationssystemUI */
	public ClientUI() {
		initComponents();

		JDialog dialogOverview = new JDialog(this, "Overview");
		dialogOverview.setBounds(200, 200, 650, 510);
		dialogOverview.setVisible(true);
		ipanel = new ImagePanel();
		dialogOverview.getContentPane().add(ipanel);


		for( int i = 0; i < 60; ++i ) {
			if( i < 24 ) {
				jComboBox4.addItem( i );
				jComboBox7.addItem( i );
			}
			jComboBox5.addItem( i );
			jComboBox8.addItem( i );
		}

		adapter = Factory.getAdapter(this);
		try {
			adapter.registerForCallback(this);
		} catch (RemoteException e) {
			e.printStackTrace();
		}


		//erzeuge beispieldaten 
		initData();

	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	// <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
	private void initComponents() {
		java.awt.GridBagConstraints gridBagConstraints;   
		pnl_Main = new javax.swing.JPanel();
		pnl_Start = new javax.swing.JPanel();
		btn_Start = new javax.swing.JButton();
		btn_LandungDurchfuehren = new javax.swing.JButton();
		btn_Exit = new javax.swing.JButton();
		pnl_LandebahnAuswaehlen = new javax.swing.JPanel();
		jLabel1 = new javax.swing.JLabel();
		jLabel2 = new javax.swing.JLabel();
		jComboBoxLandingWaySel = new javax.swing.JComboBox();
		btn_Next_StartLandebahn = new javax.swing.JButton();
		btn_Abort_StartLandebahn = new javax.swing.JButton();
		pnl_FlugzeugAuswaehlen = new javax.swing.JPanel();
		jLabel3 = new javax.swing.JLabel();
		jLabel4 = new javax.swing.JLabel();
		jLabel5 = new javax.swing.JLabel();
		jLabel6 = new javax.swing.JLabel();
		jComboBoxPlaneSel = new javax.swing.JComboBox();
		jComboBox3 = new javax.swing.JComboBox();
		jComboBox4 = new javax.swing.JComboBox();
		jComboBox5 = new javax.swing.JComboBox();
		jComboBox6 = new javax.swing.JComboBox();

		jComboBoxLandingWaySel.addItemListener(this);
		jComboBox3.addItemListener(this);
		jComboBox6.addItemListener(this);

		jLabel8 = new javax.swing.JLabel();
		btn_Abort_flugzeugAuswaehlen = new javax.swing.JButton();
		btn_Next_FlugzeugAuswaehlen = new javax.swing.JButton();
		btn_Back_FlugzeugAuswaehlen = new javax.swing.JButton();
		pnl_Freigabe = new javax.swing.JPanel();
		btn_Parkpos_Freigeben = new javax.swing.JButton();
		btn_StartbahnFreigeben = new javax.swing.JButton();
		jLabel7 = new javax.swing.JLabel();
		jComboBox7 = new javax.swing.JComboBox();
		jComboBox8 = new javax.swing.JComboBox();
		jLabel9 = new javax.swing.JLabel();
		btn_Abort_Freigabe = new javax.swing.JButton();
		btn_Finish = new javax.swing.JButton();
		btn_Back_Freigabe = new javax.swing.JButton();

		getContentPane().setLayout(new java.awt.GridBagLayout());

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		setTitle("Flughafeninformationssystem");
		pnl_Main.setLayout(new java.awt.CardLayout());

		pnl_Start.setLayout(new java.awt.GridBagLayout());

		btn_Start.setText("Start durchf\u00fchren");

		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		pnl_Start.add(btn_Start, gridBagConstraints);

		btn_LandungDurchfuehren.setText("Landung durchf\u00fchren");
		btn_LandungDurchfuehren.setEnabled(false);
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 1;
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		pnl_Start.add(btn_LandungDurchfuehren, gridBagConstraints);

		btn_Exit.setText("Beenden");

		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 2;
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		pnl_Start.add(btn_Exit, gridBagConstraints);

		pnl_Main.add(pnl_Start, "card5");

		pnl_LandebahnAuswaehlen.setLayout(new java.awt.GridBagLayout());

		jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11));
		jLabel1.setText("Landebahn Ausw\u00e4hlen");
		jLabel1.setName("lbl_CardTitle");
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridwidth = 2;
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
		pnl_LandebahnAuswaehlen.add(jLabel1, gridBagConstraints);

		jLabel2.setText("Landebahn");
		jLabel2.setName("lbl_Landebahn");
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 1;
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
		pnl_LandebahnAuswaehlen.add(jLabel2, gridBagConstraints);

		jComboBoxLandingWaySel.setName("cbx_Landebahn");
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 1;
		gridBagConstraints.gridy = 1;
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		pnl_LandebahnAuswaehlen.add(jComboBoxLandingWaySel, gridBagConstraints);
		

		btn_Next_StartLandebahn.setText("Weiter");

		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 1;
		gridBagConstraints.gridy = 2;
		pnl_LandebahnAuswaehlen.add(btn_Next_StartLandebahn, gridBagConstraints);

		btn_Abort_StartLandebahn.setText("Abbrechen");

		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 2;
		pnl_LandebahnAuswaehlen.add(btn_Abort_StartLandebahn, gridBagConstraints);

		pnl_Main.add(pnl_LandebahnAuswaehlen, "card2");

		pnl_FlugzeugAuswaehlen.setLayout(new java.awt.GridBagLayout());


		jLabel3.setText("Flugzeugtyp");
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 2;
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
		pnl_FlugzeugAuswaehlen.add(jLabel3, gridBagConstraints);

		jLabel4.setText("Fluggesellschaft");
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 1;
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
		pnl_FlugzeugAuswaehlen.add(jLabel4, gridBagConstraints);

		jLabel5.setText("Flugzeug Nr.");
		jLabel5.setName("lbl_FlugzeugNr");
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 3;
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
		pnl_FlugzeugAuswaehlen.add(jLabel5, gridBagConstraints);

		jLabel6.setText("Sollzeit");
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 4;
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
		pnl_FlugzeugAuswaehlen.add(jLabel6, gridBagConstraints);

		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 1;
		gridBagConstraints.gridy = 3;
		gridBagConstraints.gridwidth = 2;
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
		pnl_FlugzeugAuswaehlen.add(jComboBoxPlaneSel, gridBagConstraints);

		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 1;
		gridBagConstraints.gridy = 2;
		gridBagConstraints.gridwidth = 2;
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
		pnl_FlugzeugAuswaehlen.add(jComboBox3, gridBagConstraints);

		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 1;
		gridBagConstraints.gridy = 4;
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		pnl_FlugzeugAuswaehlen.add(jComboBox4, gridBagConstraints);

		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 2;
		gridBagConstraints.gridy = 4;
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		pnl_FlugzeugAuswaehlen.add(jComboBox5, gridBagConstraints);

		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 1;
		gridBagConstraints.gridy = 1;
		gridBagConstraints.gridwidth = 2;
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
		pnl_FlugzeugAuswaehlen.add(jComboBox6, gridBagConstraints);

		jLabel8.setText("Flugzeug Ausw\u00e4hlen");
		jLabel8.setName("lbl_CardTitle");
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 0;
		gridBagConstraints.gridwidth = 3;
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
		pnl_FlugzeugAuswaehlen.add(jLabel8, gridBagConstraints);

		btn_Abort_flugzeugAuswaehlen.setText("Abbrechen");

		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 5;
		pnl_FlugzeugAuswaehlen.add(btn_Abort_flugzeugAuswaehlen, gridBagConstraints);

		btn_Next_FlugzeugAuswaehlen.setText("Weiter");

		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 2;
		gridBagConstraints.gridy = 5;
		pnl_FlugzeugAuswaehlen.add(btn_Next_FlugzeugAuswaehlen, gridBagConstraints);

		btn_Back_FlugzeugAuswaehlen.setText("Zur\u00fcck");

		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 1;
		gridBagConstraints.gridy = 5;
		pnl_FlugzeugAuswaehlen.add(btn_Back_FlugzeugAuswaehlen, gridBagConstraints);

		pnl_Main.add(pnl_FlugzeugAuswaehlen, "card3");

		pnl_Freigabe.setLayout(new java.awt.GridBagLayout());

		btn_Parkpos_Freigeben.setText("Parkposition freigeben");
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 1;
		gridBagConstraints.gridwidth = 3;
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
		pnl_Freigabe.add(btn_Parkpos_Freigeben, gridBagConstraints);

		btn_StartbahnFreigeben.setText("Startbahn freigeben");
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 2;
		gridBagConstraints.gridwidth = 3;
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
		pnl_Freigabe.add(btn_StartbahnFreigeben, gridBagConstraints);

		jLabel7.setText("Istzeit");
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 3;
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
		pnl_Freigabe.add(jLabel7, gridBagConstraints);

		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 1;
		gridBagConstraints.gridy = 3;
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.weightx = 0.1;
		pnl_Freigabe.add(jComboBox7, gridBagConstraints);

		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 2;
		gridBagConstraints.gridy = 3;
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.weightx = 0.1;
		pnl_Freigabe.add(jComboBox8, gridBagConstraints);

		jLabel9.setText("Freigabe");
		jLabel9.setName("lbl_CardTitle");
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 0;
		gridBagConstraints.gridwidth = 3;
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
		pnl_Freigabe.add(jLabel9, gridBagConstraints);

		btn_Abort_Freigabe.setText("Abbrechen");

		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 4;
		pnl_Freigabe.add(btn_Abort_Freigabe, gridBagConstraints);

		btn_Finish.setText("Fertig");

		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 2;
		gridBagConstraints.gridy = 4;
		pnl_Freigabe.add(btn_Finish, gridBagConstraints);

		btn_Back_Freigabe.setText("Zur\u00fcck");
		
		btn_Start.addActionListener( this );
		btn_Parkpos_Freigeben.addActionListener( this );
		btn_StartbahnFreigeben.addActionListener( this );
		btn_LandungDurchfuehren.addActionListener( this );
		btn_Next_StartLandebahn.addActionListener( this );
		btn_Abort_StartLandebahn.addActionListener( this );
		btn_Abort_Freigabe.addActionListener( this );
		btn_Finish.addActionListener( this );
		btn_Abort_flugzeugAuswaehlen.addActionListener( this );
		btn_Next_FlugzeugAuswaehlen.addActionListener( this );
		btn_Exit.addActionListener( this );
		btn_Back_FlugzeugAuswaehlen.addActionListener( this );
		btn_Back_Freigabe.addActionListener(this);
		
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 1;
		gridBagConstraints.gridy = 4;
		pnl_Freigabe.add(btn_Back_Freigabe, gridBagConstraints);

		pnl_Main.add(pnl_Freigabe, "card4");

		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 0;
		gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
		gridBagConstraints.ipadx = 8;
		gridBagConstraints.ipady = 8;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
		getContentPane().add(pnl_Main, gridBagConstraints);

		pack();
	}// </editor-fold>//GEN-END:initComponents

	private void btn_ExitClicked(ActionEvent evt) {//GEN-FIRST:event_btn_ExitClicked
		try {
			adapter.unregisterFromCallback(this);
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		System.exit(0);
	}//GEN-LAST:event_btn_ExitClicked

	private void btn_CancelClicked(ActionEvent evt) {//GEN-FIRST:event_btn_CancelClicked
		if (this.selectedLandingWayId != -1){
			try{
			adapter.startBahnBelegungAndern(this.selectedLandingWayId, true);
			this.selectedLandingWayId = -1;
			} catch (RemoteException re){
				re.printStackTrace();
			}
		}
		CardLayout cl = (CardLayout)(pnl_Main.getLayout());
		cl.first(pnl_Main);
	}//GEN-LAST:event_btn_CancelClicked

	private void btn_NextClicked(ActionEvent evt) {//GEN-FIRST:event_btn_NextClicked
		if (evt.getSource() == btn_Next_StartLandebahn){
			try {				
				this.selectedLandingWayId = ((StartLandebahn)jComboBoxLandingWaySel.getSelectedItem()).getID();
				adapter.startBahnBelegungAndern(this.selectedLandingWayId, false);
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}			
		if( evt.getSource() == btn_Next_FlugzeugAuswaehlen) {
			Flugzeug f;
			try {				
				f = adapter.getFlugzeug(Integer.parseInt(jComboBoxPlaneSel.getSelectedItem().toString()));
				this.selectedFlugzeugId = f.getID();	
				System.out.println("Fl-Id: " + f.getID() + "ParkId: " + f.getParkposition().getID());			
				Fluggesellschaft fg = f.getFluggesellschaft();
				String sollzeit = jComboBox4.getSelectedItem().toString() + jComboBox5.getSelectedItem().toString();
				adapter.startEinplanen(this.selectedLandingWayId, fg.getID(), f.getID(), sollzeit);
			} catch (RemoteException e) {
				e.printStackTrace();
			}			
			btn_Parkpos_Freigeben.setEnabled(false);
			btn_Finish.setEnabled(false);
			btn_StartbahnFreigeben.setEnabled(false);
			
		}
		
		if( evt.getSource() == btn_Finish){			
			try {
				adapter.finishStartProcess(this.selectedFlugzeugId);
				this.selectedFlugzeugId = -1;
				this.selectedLandingWayId = -1;		
			} catch (RemoteException e) {				
				e.printStackTrace();
			}
		}
		
		CardLayout cl = (CardLayout)(pnl_Main.getLayout());
		cl.next(pnl_Main);
	}//GEN-LAST:event_btn_NextClicked
	
	private void btn_PrevClicked(ActionEvent evt) {//GEN-FIRST:event_btn_PrevClicked
		// reset the landing way reservation
		if (evt.getSource() == btn_Back_FlugzeugAuswaehlen){
			try {	
				jComboBoxLandingWaySel.setSelectedIndex(((StartLandebahn)jComboBoxLandingWaySel.getSelectedItem()).getID());
				adapter.startBahnBelegungAndern(((StartLandebahn)jComboBoxLandingWaySel.getSelectedItem()).getID(), true);			
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}
		CardLayout cl = (CardLayout)(pnl_Main.getLayout());
		cl.previous(pnl_Main);
	}//GEN-LAST:event_btn_PrevClicked

	@Override
	public void update(IAdapter adapter) throws RemoteException {
		ipanel.repaint();
		/* update content of ComboBoxes, prevent changing the selection of the landing way. Note: could cause difficulties
		 * if landing ways are added / removed, but hey that won't happen anyway so whatever */
		int selectedIndex = jComboBoxLandingWaySel.getSelectedIndex();
		jComboBoxLandingWaySel.removeAllItems();		
		for(StartLandebahn startLandebahn : adapter.getStartLandebahnen()){
			jComboBoxLandingWaySel.addItem(startLandebahn);
		}		
		jComboBoxLandingWaySel.setSelectedIndex(selectedIndex);
		
		/* set the state of the StartBahnFreigeben and ParkpositionFreigeben buttons, if possible do a check for 
		 * the active page to be set correspondingly (dunno how, lol) */
		boolean isButtonActive = false;
		if (this.selectedFlugzeugId != -1){
			Map<Integer, Boolean> sensorValues = adapter.getParkingPositionSensors();
			Flugzeug f = adapter.getFlugzeug(this.selectedFlugzeugId);
			for (Integer i : sensorValues.keySet()){
				if (i.intValue() == f.getParkposition().getID()){
					/* sensor = true means there is something on the parking position */
					isButtonActive = !sensorValues.get(i);
					System.out.println("Parkpos " + i + " " + isButtonActive);
					break;
				}
			}
			btn_Parkpos_Freigeben.setEnabled(isButtonActive);
		}		
		if (this.selectedLandingWayId != -1){
			// to be safe set the index of the ComboBox to the value used before using the object behind the selection
			jComboBoxLandingWaySel.setSelectedIndex(this.selectedLandingWayId);
			Map<Integer, Boolean> sensorValues = adapter.getLandingLaneSensors();
			for (Integer i : sensorValues.keySet()){
				if (i.intValue() == this.selectedLandingWayId){
					isButtonActive = !sensorValues.get(i);
					System.out.println("Landebahn "+ i + " " + isButtonActive);
					break;
				}
			}
			btn_StartbahnFreigeben.setEnabled(isButtonActive);
			btn_Finish.setEnabled(isButtonActive);
			btn_Back_Freigabe.setEnabled(!isButtonActive);
			btn_Abort_Freigabe.setEnabled(!isButtonActive);
		}
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		System.out.println(e.getActionCommand());		
		if( e.getSource() == btn_Parkpos_Freigeben ) {
				try {
					adapter.setParkpositionFree(this.selectedFlugzeugId, adapter.getFlugzeug(this.selectedFlugzeugId).getParkposition().getID());
				} catch (RemoteException e1) {
					e1.printStackTrace();
				}
		} else if( e.getSource() == btn_StartbahnFreigeben ) {
			try {
				adapter.startBahnBelegungAndern(this.selectedLandingWayId, true);
			} catch (RemoteException e1) {			
				e1.printStackTrace();
			} 
		} else if( e.getSource() == btn_Next_StartLandebahn 
				|| e.getSource() == btn_Next_FlugzeugAuswaehlen
				|| e.getSource() == btn_Finish
				|| e.getSource() == btn_Start
				|| e.getSource() == btn_LandungDurchfuehren) {				
			btn_NextClicked( e );
		} else if( e.getSource() == btn_Abort_flugzeugAuswaehlen 
				|| e.getSource() == btn_Abort_Freigabe
				|| e.getSource() == btn_Abort_StartLandebahn) {			
			btn_CancelClicked(e);
		} else if( e.getSource() == btn_Exit ) {
			btn_ExitClicked( e );
		} else if( e.getSource() == btn_Back_FlugzeugAuswaehlen 
				|| e.getSource() == btn_Back_Freigabe ) {
			btn_PrevClicked(e);
		}
	}
	
	@Override
	public void itemStateChanged(ItemEvent arg0) {
		
		if(arg0.getSource() == jComboBoxLandingWaySel){
			if( arg0.getStateChange() == ItemEvent.SELECTED ) {
				btn_Next_StartLandebahn.setEnabled(((StartLandebahn)arg0.getItem()).isFree());
			}
		}
		if(arg0.getSource() == jComboBox3){
			if( arg0.getStateChange() == ItemEvent.SELECTED ) {
				jComboBoxPlaneSel.removeAllItems();
				if( jComboBox6.getItemCount() != 0) {
					Fluggesellschaft fg =(Fluggesellschaft)( jComboBox6.getSelectedItem());
					for (Flugzeug f : fg.getFlugzeuge() ) {
						jComboBoxPlaneSel.addItem( f.getID() );						
					}
				}
			}
		}
		if(arg0.getSource() == jComboBox6){
			if( arg0.getStateChange() == ItemEvent.SELECTED ) {
				jComboBox3.removeAllItems();
				Fluggesellschaft fg;
				boolean unique = true; 
				fg = (Fluggesellschaft)( arg0.getItem());
				for ( Flugzeug f : fg.getFlugzeuge() ) {
					for ( int i = 0; i < jComboBox3.getItemCount(); ++i ) {
						if( jComboBox3.getItemAt( i ).equals( f.getFlugzeugtyp() ) ) {
							unique = false;
						}
					}
					if( unique == true ) {
						jComboBox3.addItem( f.getFlugzeugtyp() );
						jComboBox3.setSelectedItem( f.getFlugzeugtyp() );
					}
				}
			}
		}
		
	}

	private void initData(){

		try {
			int flugzeugId, fgesellschaftId, typId;
			//quick and dirty
			if(adapter.getFluggesellschaften().size()  == 0){
				adapter.createStartLandebahn("Startlandebahn 1");
				adapter.createStartLandebahn("Startlandebahn 2");
				adapter.createStartLandebahn("Startlandebahn 3");	
				adapter.createStartLandebahn("Startlandebahn 4");	

				int parkId1 = adapter.createParkposition("Parkpostion 1");
				int parkId2 = adapter.createParkposition("Parkpostion 2");
				int parkId3 = adapter.createParkposition("Parkpostion 3");
				int parkId4 = adapter.createParkposition("Parkpostion 4");
				int parkId5 = adapter.createParkposition("Parkpostion 5");
				int parkId6 = adapter.createParkposition("Parkpostion 6");
				adapter.createParkposition("Parkpostion 7");
				adapter.createParkposition("Parkpostion 8");
				adapter.createParkposition("Parkpostion 9");
				adapter.createParkposition("Parkpostion A");
				adapter.createParkposition("Parkpostion B");
				adapter.createParkposition("Parkpostion C");


				//alle flugzeuge auf Parkposition
				fgesellschaftId = adapter.createFluggesellschaft("Lufthansa");
				typId = adapter.createFlugzeugtyp("lufthansaTyp", 1000);

				flugzeugId = adapter.createFlugzeug("LufthansaFlugzeug1", fgesellschaftId, typId);
				jComboBoxPlaneSel.addItem(flugzeugId);
				adapter.setParkpositionOcc(flugzeugId, parkId1);
				flugzeugId = adapter.createFlugzeug("LufthansaFlugzeug2", fgesellschaftId, typId);
				jComboBoxPlaneSel.addItem(flugzeugId);
				adapter.setParkpositionOcc(flugzeugId, parkId2);

				fgesellschaftId = adapter.createFluggesellschaft("Emirates");
				typId = adapter.createFlugzeugtyp("EmiratesTyp", 1500);
				flugzeugId = adapter.createFlugzeug("EmiratesFlugzeug1", fgesellschaftId, typId);
				adapter.setParkpositionOcc(flugzeugId, parkId3);
				flugzeugId = adapter.createFlugzeug("EmiratesFlugzeug2", fgesellschaftId, typId);
				adapter.setParkpositionOcc(flugzeugId, parkId4);

				fgesellschaftId = adapter.createFluggesellschaft("LaudaAir");
				typId = adapter.createFlugzeugtyp("LaudaAirTyp", 2000);
				flugzeugId = adapter.createFlugzeug("LaudaAirFlugzeug1", fgesellschaftId, typId);
				adapter.setParkpositionOcc(flugzeugId, parkId5);
				flugzeugId = adapter.createFlugzeug("LaudaAirFlugzeug2", fgesellschaftId, typId);
				adapter.setParkpositionOcc(flugzeugId, parkId6);

			}
			for(Flugzeug flugzeug : adapter.getFlugzeuge()){
				jComboBoxPlaneSel.addItem(flugzeug.getID());
				jComboBox3.addItem(flugzeug.getFlugzeugtyp());
			}
			for(StartLandebahn startLandebahn : adapter.getStartLandebahnen()){
				jComboBoxLandingWaySel.addItem(startLandebahn);
			}
			for(Fluggesellschaft fluggesellschaft : adapter.getFluggesellschaften()){
				jComboBox6.addItem(fluggesellschaft);
			}


		} catch (RemoteException e) {
			e.printStackTrace();
		}


	}


	/**
	 * @param args the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new ClientUI().setVisible(true);

			}
		});
	}

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private static IAdapter adapter;
	private javax.swing.JButton btn_Start;
	private javax.swing.JButton btn_Next_FlugzeugAuswaehlen;
	private javax.swing.JButton btn_Exit;
	private javax.swing.JButton btn_Back_FlugzeugAuswaehlen;
	private javax.swing.JButton btn_Back_Freigabe;
	private javax.swing.JButton btn_Parkpos_Freigeben;
	private javax.swing.JButton btn_StartbahnFreigeben;
	private javax.swing.JButton btn_LandungDurchfuehren;
	private javax.swing.JButton btn_Next_StartLandebahn;
	private javax.swing.JButton btn_Abort_StartLandebahn;
	private javax.swing.JButton btn_Abort_Freigabe;
	private javax.swing.JButton btn_Finish;
	private javax.swing.JButton btn_Abort_flugzeugAuswaehlen;
	private javax.swing.JComboBox jComboBoxLandingWaySel;
	private javax.swing.JComboBox jComboBoxPlaneSel;
	private javax.swing.JComboBox jComboBox3;
	private javax.swing.JComboBox jComboBox4;
	private javax.swing.JComboBox jComboBox5;
	private javax.swing.JComboBox jComboBox6;
	private javax.swing.JComboBox jComboBox7;
	private javax.swing.JComboBox jComboBox8;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JLabel jLabel4;
	private javax.swing.JLabel jLabel5;
	private javax.swing.JLabel jLabel6;
	private javax.swing.JLabel jLabel7;
	private javax.swing.JLabel jLabel8;
	private javax.swing.JLabel jLabel9;
	private javax.swing.JPanel pnl_FlugzeugAuswaehlen;
	private javax.swing.JPanel pnl_Freigabe;
	private javax.swing.JPanel pnl_LandebahnAuswaehlen;
	private javax.swing.JPanel pnl_Main;
	private javax.swing.JPanel pnl_Start;
	
}
